package org.example.SolutionNew003;

public class DoubleTree003 {
    public static void main(String[] args) {
        DoubleTree003 dt = new DoubleTree003();
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        TreeNode res=dt.invertTree(root);
        System.out.println(res.val);
        System.out.println(res.left.val);
        System.out.println(res.right.val);
        System.out.println(res.left.left.val);
        System.out.println(res.left.right.val);
    }

    public TreeNode invertTree1(TreeNode root) {
        if (root == null|| (root.left == null && root.right == null)) return root;
        if (root.left != null) invertTree1(root.left);
        if (root.right != null) invertTree1(root.right);
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        return root;
    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null) return root;
        TreeNode left = invertTree(root.left);
        TreeNode right = invertTree(root.right);
        root.left = right;
        root.right = left;
        return root;
    }
}
